package com.users.rn.rncommon.manager;

import android.app.Application;
import com.users.rn.common.plugin.PluginHelper;
import com.facebook.infer.annotation.Assertions;
import com.facebook.soloader.SoLoader;
import com.users.rn.rncommon.model.PluginInfo;


/**
 * @Description:
 * @Author: bluce
 * @Date: 2020/12/3 10:48
 */
public final class ALHReactEnvManager {
    private static final String ERROR_MESSAGE = "ALHReactEnvManager.getInstance().init() method must be called first!";

    private static final ALHReactEnvManager mInstance = new ALHReactEnvManager();

    public static ALHReactEnvManager getInstance() {
        return mInstance;
    }

    private ALHReactHostManager mReactHostManager;


    /**
     * rn初始化
     *
     * @param application
     */
    public void init(Application application) {
        this.init(application, false);
    }

    public void init(Application application, boolean nativeExopackage) {
        SoLoader.init(application, /* native exopackage */ nativeExopackage);
        mReactHostManager = new ALHReactHostManager(application);
        //如果这里检查到没有做RN下载配置初始化，则进行初始化
        PluginHelper.INSTANCE.initPlugin(application);
//        if (!PluginManager.getInstance().isInitializedLocalData()) {
//            RNLogger.i("RnPluginProgressActivity onResume: RN local data not init");
//            File file = application.getExternalFilesDir("plugin");
//            if (null != file) {
//                String filePath = file.getAbsolutePath();
//                try {
//                    PluginManager.getInstance().initLocalData(application, filePath, ALHReactNativeInit.INSTANCE.getInitInterface().getDownloadUrl());
//                } catch (Exception e) {
//                    e.printStackTrace();
//                }
//            }
//        }
    }

    /**
     * 获取公包
     *
     * @return
     */
    public ALHReactNativeHost getCommonReactNativeHost() {
        return Assertions.assertNotNull(mReactHostManager, ERROR_MESSAGE).getCommonReactNativeHost();
    }

    /**
     * 获取指定业务bundle
     *
     * @param pluginInfo
     * @return
     */
    public ALHReactNativeHost getReactNativeHost(PluginInfo pluginInfo) {
        return Assertions.assertNotNull(mReactHostManager, ERROR_MESSAGE).getBizReactNativeHost(pluginInfo);
    }

    public void removeReactNativeHost(String pluginId) {
        Assertions.assertNotNull(mReactHostManager, ERROR_MESSAGE).removeBizReactNativeHost(pluginId);
    }

    public ALHReactNativeHost getDebugReactNativeHost() {
        return Assertions.assertNotNull(mReactHostManager, ERROR_MESSAGE).getDebugHost();
    }
}
